package com.project.yunshitong.utils;

import static android.content.ContentValues.TAG;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.project.yunshitong.R;


public class LoadingDialog extends Dialog {
    private static final int CHANGE_TITLE_WHAT = 1;
    private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
    private static final int MAX_SUFFIX_NUMBER = 3;
    private static final char SUFFIX = '.';
    private Context context;
    private LinearLayout ll_loading;
    private ImageView iv_route;
    private TextView tv;
    private TextView tv_point;
    private RelativeLayout rl_loading;
    private ProgressBar pbProgress;
    private TextView tvProgress;
    private RotateAnimation mAnim;
    private boolean cancelable = true;
    private String msg = "";

    private Handler handler = new Handler() {
        private int num = 0;

        @Override
        public void handleMessage(android.os.Message msg) {
            if (msg.what == CHANGE_TITLE_WHAT) {
                StringBuilder builder = new StringBuilder();
                if (num >= MAX_SUFFIX_NUMBER) {
                    num = 0;
                }
                num++;
                for (int i = 0; i < num; i++) {
                    builder.append(SUFFIX);
                }
                tv_point.setText(builder.toString());
                if (isShowing()) {
                    handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS);
                } else {
                    num = 0;
                }
            }
        }

        ;
    };

    public LoadingDialog(Context context) {
        super(context, R.style.Dialog_bocop);//
        this.context = context;
        touming();
        init(1);
    }

    public LoadingDialog(Context context, String msg, int type) {
        super(context, R.style.Dialog_bocop);//
        this.msg = msg;
        this.context = context;
//        touming();
        init(type);
    }

    public void setProgress(int progress, int maxProgress) {
        double p = progress * 100 / maxProgress;
//        Log.e(TAG, "setProgress: p========"+p );
        pbProgress.setProgress((int) p);
        tvProgress.setText(progress + "/" + maxProgress);
    }

    private void init(int type) {
        View contentView = View.inflate(context, R.layout.activity_custom_loding_dialog_layout, null);
        setContentView(contentView);

        contentView.setOnClickListener(v -> {
            if (cancelable) {
                dismiss();
            }
        });
        iv_route = (ImageView) findViewById(R.id.iv_route);
        tv = (TextView) findViewById(R.id.tv);
        tv_point = (TextView) findViewById(R.id.tv_point);
        ll_loading = findViewById(R.id.ll_loading);
        rl_loading = findViewById(R.id.rl_loading);
        tvProgress = findViewById(R.id.tvProgress);
        pbProgress = findViewById(R.id.pbProgress);
        if (type == 1) {
            tvProgress.setVisibility(View.GONE);
            pbProgress.setVisibility(View.GONE);
        } else {
            tvProgress.setVisibility(View.VISIBLE);
            pbProgress.setVisibility(View.VISIBLE);
        }
        initAnim();
        ll_loading.setOnClickListener(view -> {

        });
        if (!TextUtils.isEmpty(msg)) {
            tv.setText(msg);
            rl_loading.setVisibility(View.VISIBLE);
        } else {
            rl_loading.setVisibility(View.GONE);
        }
        //getWindow().setWindowAnimations(R.anim.alpha_in);
    }

    private void touming() {
        //透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    private void initAnim() {
        mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
        mAnim.setDuration(6000);
        mAnim.setRepeatCount(Animation.INFINITE);
        mAnim.setRepeatMode(Animation.RESTART);
        mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
    }

    @Override
    public void show() {
        Log.e(TAG, "show: 显示了吗=========="+(getWindow().getWindowManager() == null) );
        if (getWindow().getWindowManager() == null) {
            return;
        }
        iv_route.startAnimation(mAnim);
        handler.sendEmptyMessage(CHANGE_TITLE_WHAT);
        super.show();
    }

    @Override
    public void dismiss() {
        if (getWindow().getWindowManager() == null) {
            return;
        }
        mAnim.cancel();
        if (context != null && !((Activity) context).isFinishing()) {
            super.dismiss();
        }
    }

    @Override
    protected void onStop() {
        if (getWindow().getWindowManager() == null) {
            return;
        }
        mAnim.cancel();
        if (context != null && !((Activity) context).isFinishing()) {
            dismiss();
        }
        super.onStop();
    }

    @Override
    public void setCancelable(boolean flag) {
        cancelable = flag;
        super.setCancelable(flag);
    }

    @Override
    public void setTitle(CharSequence title) {
        tv.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getString(titleId));
    }
}
